﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using NCS.Library.Components.Portal;
using NCS.Library.Components.Roles;
using NCS.Library.Components.Security;
using NCS.Library.Components.Users;
using NCS.Library.Utilities;
using NCS.Library.Components.Partner;
using System.Xml;
namespace Portal.Administrator.Users
{
    public partial class CreateUser : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CurrentUser.IsAdmin)
            {
                pnlAdmin.Visible = false;
            }
            if (!CurrentUser.IsSuperAdmin)
            {
                pnlSuperadmin.Visible = false;
            }
            else if (CurrentUser.IsSuperAdmin)
            {
                pnlSuperadmin.Visible = true;
                pnlAdmin.Visible = true;
            }

            if (!Page.IsPostBack)
            {
                this.loadCity();

                PortalController.LoadPortals(lstPortal.Items);
                dropPartner.DataSource = PartnerController.GetPartners();
                dropPartner.DataBind();
                if (lstPortal.Items.Count > 0)
                {
                    lstPortal.SelectedIndex = 0;
                }
            }
        }

        private void loadCity()
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath("Users/Province.xml"));
            XmlNodeList pros = xmldoc.DocumentElement.SelectNodes("./pro");

            this.dropCity.Items.Clear();

            for (int i = 0; i < pros.Count; i++)
            {
                this.dropCity.Items.Add(new ListItem(pros[i].SelectSingleNode("./text()[1]").Value, (i + 1).ToString()));
            }
        }
        private void Reset()
        {
            txtUsername.Text = "";
            txtAddress1.Text = "";
            dropCity.SelectedIndex = 0;
            //dropCountry.SelectedIndex = 0;
            txtDisplayName.Text = "";
            txtEmail.Text = "";
            txtFirstName.Text = "";
            chkIsAdmin.Checked = false;
            chkIsSuperAdmin.Checked = false;
            txtLastName.Text = "";
            txtMobile.Text = "";
            txtPassword.Text = "";
            txtPasswordConfirm.Text = "";
            txtTel.Text = "";
            txtZipCode.Text = "";            
        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (txtPassword.Text.Trim() != txtPasswordConfirm.Text.Trim()) return;
                try
                {
                    UserInfo user = new UserInfo();
                    user.Username = txtUsername.Text.Trim();
                    user.Address1 = txtAddress1.Text.Trim();
                    user.Address2 = "";
                    user.City = ConvertUtility.ToInt32(dropCity.SelectedItem.Value);
                    user.Country = ConvertUtility.ToInt32(dropCountry.SelectedValue);
                    user.DisplayName = txtDisplayName.Text.Trim();
                    user.Email = txtEmail.Text;
                    user.FirstName = txtFirstName.Text.Trim();
                    user.IsAdmin = chkIsAdmin.Checked;
                    user.IsSuperAdmin = chkIsSuperAdmin.Checked;
                    user.LastName = txtLastName.Text.Trim();
                    user.Mobile = txtMobile.Text.Trim();
                    user.Password = SecurityMethod.MD5Encrypt(txtPassword.Text.Trim());
                    user.Tel = txtTel.Text.Trim();
                    user.ZipCode = txtZipCode.Text.Trim();
                    user.PartnerID = ConvertUtility.ToInt32(dropPartner.SelectedValue);
                    user.UserType = Convert.ToInt32(this.drl_usertype.SelectedIndex);
                    user.Birthday = DateTime.Now;
                    int userId = UserController.AddUser(user);

                    if (userId > 0)
                    {
                        foreach (ListItem item in lstPortal.Items)
                        {
                            if (item.Selected)
                            {
                                RoleController.AddUserToRole(userId, AppEnv.DEFAULT_ROLE, ConvertUtility.ToInt32(item.Value));
                            }
                        }
                        lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;

                        Reset();

                        this.AddLog("User", " try create user " + txtUsername.Text + " success");
                    }
                    else
                    {
                        this.lblUpdateStatus.Text = "Có lỗi: tên đăng nhập đã tồn tại";
                    }
                }
                catch (Exception ex)
                {
                    //Response.Write(ex.Message);
                    this.lblUpdateStatus.Text = ex.Message;
                    this.AddLog("User", " try create user " + txtUsername.Text + " error: " + ex.Message);
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
           // Response.Redirect(AppEnv.ADMIN_CMD + "usermanager");
            Reset();
        }
    }
}